#include "krautchanagent.h"
#include "components/chan.h"

namespace Wishmaster
{
	KrautchanAgent::KrautchanAgent(const QString& host) : DefaultAgent(host), m_host(host)
	{
	}

	KrautchanAgent::~KrautchanAgent()
	{
	}

	void KrautchanAgent::getCaptcha(const ThreadId& threadId)
	{
		Q_UNUSED(threadId);
		// I'm not aware of captcha on the krautchan
	}

	void KrautchanAgent::makePost(const PostDescriptor& post, const QString& captcha)
	{
		Q_UNUSED(captcha);

		QString plainboardname = post.board;
		plainboardname.remove('/');

		QString referer = makeThreadLink(post.parent_thread);
		HttpRequestPtr request = connector()->makeHttpRequest(m_host, "/post", "POST");
		request->setReferer(referer);

		request->addPostField(defaultedString(property("field1name").toString(), "internal_n"), post.name);
		request->addPostField(defaultedString(property("field3name").toString(), "internal_s"), post.topic);
		if(post.sage)
			request->addPostField("sage", "1");
		request->addPostField(defaultedString(property("field4name").toString(), "internal_t"), post.text);

		if(post.attachments.count() > 0)
		{
			for(int i = 0; i < post.attachments.count(); i++)
			{
				addAttachments(request, post.attachments.at(i), QString("file_%1").arg(i));
			}
		}

		request->addPostField("forward", "thread");
		request->addPostField("password", post.password);

		request->addPostField("board", plainboardname);
		request->addPostField("parent", post.parent_thread.threadStringId());

		request->setProperty("board", post.board);
		request->setProperty("threadid", (uint)post.parent_thread.threadNumericId());

		connector()->connectRequest(this, SLOT(resultPost(RequestPtr, const QByteArray&)), 0, request);
		connector()->queue(request);
	}

	void KrautchanAgent::createThread(const PostDescriptor& post, const QString& captcha)
	{
		Q_UNUSED(captcha);

		QString plainboardname = post.board;
		plainboardname.remove('/');

		QString referer = makeThreadLink(post.parent_thread);
		HttpRequestPtr request = connector()->makeHttpRequest(m_host, "/post", "POST");
		request->setReferer(referer);

		request->addPostField(defaultedString(property("field1name").toString(), "internal_n"), post.name);
		request->addPostField(defaultedString(property("field3name").toString(), "internal_s"), post.topic);
		if(post.sage)
			request->addPostField("sage", "1");
		request->addPostField(defaultedString(property("field4name").toString(), "internal_t"), post.text);

		if(post.attachments.count() > 0)
		{
			for(int i = 0; i < post.attachments.count(); i++)
			{
				addAttachments(request, post.attachments.at(i), QString("file_%1").arg(i));
			}
		}

		request->addPostField("forward", "thread");
		request->addPostField("password", post.password);

		request->addPostField("board", plainboardname);
		request->setProperty("board", post.board);
		request->setProperty("threadid", post.board);

		connector()->connectRequest(this, SLOT(resultPost(RequestPtr, const QByteArray&)), 0, request);
		connector()->queue(request);
	}

	void KrautchanAgent::deleteThread(const ThreadId& threadId, const QString& password)
	{
	}

	void KrautchanAgent::deletePost(const PostId& postId, const QString& password)
	{
	}

	QStringList KrautchanAgent::ratings()
	{
		return QStringList();
	}

	ChanAgent* KrautchanAgent::clone(const QString& host)
	{
		return new KrautchanAgent(host);
	}

	QString KrautchanAgent::id()
	{
		return "krautchan";
	}

	void KrautchanAgent::resultPost(const HttpRequestPtr& r, const QByteArray& data)
	{
		Q_UNUSED(data);
		if(r->error())
		{
			emit postingError("");
			return;
		}
		emit postingDone(ThreadId(m_host, r->property("board").toString(), r->property("threadid").toUInt()));
	}


	QString KrautchanAgent::defaultedString(const QString& str, const QString& def)
	{
		if(str.isEmpty())
			return def;
		return str;
	}
}

Q_EXPORT_PLUGIN2(krautchanagent, Wishmaster::KrautchanAgent)

